<?php
/**
 * 验证码相关
 * User: wuhao
 * Date: 2017/5/5
 * Time: 10:05
 */
namespace app\wxbackend\controller;

use app\wxbackend\controller\Wx\WxSession;
use app\wxbackend\controller\Wx\WxTags;
use app\wxbackend\model\Member as WxMember;
use Business\Business;
use Business\sms\VComSms;
use think\Controller;

class VerifyCodeController extends Controller
{
    public function __construct()
    {
    }

    /**
     * 发送验证码
     * @date   2017-05-04
     * @author wuhao
     *
     * @param  int     $mobile         手机号
     * @param  string  $key            reg 注册验证码;
     *                                  change_mobile 更换手机号验证码;
     *                                  reset_pwd 重置密码验证;
     *                                  alipay 更新支付宝账号;
     *                                  withdraw_pwd 更新提现密码;
     *                                  bank_card    绑定银行卡
     *                                  bank_card_del 删除银行卡
     *
     * @return
     */
    private function sendMobileCode($mobile, $key)
    {
        $business = new Business();
        $res = $business->sendCode($mobile, $key);

        if ($res['code'] == 200) {
            $this->apiReturn($res['code'], [], '发送成功');
        } elseif ($res['code'] == 141) {
            $this->apiReturn($res['code'], [], '发送间隔低于2分钟');
        } else {
            $this->apiReturn($res['code'], [], '发送失败');
        }
    }

    /**
     * @comment 微信绑定发送验证码
     * @author wuhao
     */
    public function sendBindWxCode()
    {
        $mobile = I('mobile', '', 'strval');

        if (!ismobile($mobile)) {
            $this->apiReturn(405, [], '手机号格式错误');
        }
        $wxMember = new WxMember();
        $info = $wxMember->getMemberByMobile($mobile);
        if ($info) {
            $this->apiReturn(404, [], '手机号已经绑定过了');
        }
        $this->sendMobileCode($mobile, 'wx_bind');
    }

    /**
     * @comment 检验微信绑定验证码
     * @author wuhao
     */
    public function checkWxBindCode()
    {
        $openid     = I('openid', '', 'strval');
        $mobile     = I('mobile', '', 'strval');
        $mobileCode = I('mobile_code', '', 'strval');
        if (!$mobileCode || !$mobile) {
            $this->apiReturn(405, [], '缺少参数');
        }
        if (!$openid) {
            $this->apiReturn(405, [], '缺少openid');
        }
        //检查验证码
        $sms = new VComSms();
        $check = $sms->ChkVerifyCode($mobile, 'wx_bind', $mobileCode);
        if (!$check) {
            $this->apiReturn(403, [], '手机验证码错误');
        } elseif ($check === -1) {
            $this->apiReturn(403, [], '验证码已经失效');
        } else { //成功，进行绑定
            $memberModel = new WxMember();
            //检测是否已绑定
            $wxInfo = $memberModel->getMemberByMobile($mobile);
            if ($wxInfo && $wxInfo['status'] == 1) {
                $code = 201;
                $content = '您已绑定账号，无需重复绑定';
            } else {

                //获取账号信息
                $memberInfo = $memberModel->getMemberInfoByMobile($mobile);
                if (!$memberInfo) {
                    $code = 202;
                    $content = '手机号尚未绑定平台账号';
                } else {
                    $where = [
                        'openid'  => $openid,
                    ];
                    if ($memberInfo['dtype'] != 7) {
                        $dtype = $pdtype = $memberInfo['dtype'];
                        $pmid = $mid = $memberInfo['id'];
                    } else {//员工
                        //获取父账号
                        $parent = $memberModel->getParentId($memberInfo['id']);
                        $parentInfo = $memberModel->getMemberInfoById($parent['parent_id']);
                        $dtype = $memberInfo['dtype'];
                        $pdtype = $parentInfo ? $parentInfo['dtype'] : '';
                        $pmid = $parentInfo ? $parentInfo['id'] : '';
                    }
                    $data = [
                        'mid' => $memberInfo['id'],
                        'pmid' => $pmid,
                        'mobile' => $memberInfo['mobile'],
                        'account' => $memberInfo['username'],
                        'status' => 1,
                        'dtype' => $dtype,
                        'pdtype' => $pdtype,
                        'update_time' => date('Y-m-d H:i:s'),
                    ];
                    if ($memberModel->updateInfo($where, $data)) {
                        $code = 200;
                        $content = '您已成功绑定账号'.$memberInfo['username'];

                        //更新用户对应菜单
                        if ($pdtype == 2) {//分为租户组
                            WxTags::setTags([$openid], 102);
                        } else if ($pdtype == 0) { //管理员组
                            WxTags::setTags([$openid], 104);
                        } else {  //分为收款方组
                            WxTags::setTags([$openid], 103);
                        }

                        WxSession::setSession($openid);
                        session('mid', $memberInfo['id']);
                        session('pmid', $pmid);
                        session('account', $memberInfo['username']);
                        session('mobile', $memberInfo['mobile']);
                        session('status', 1);
                        session('dtype', $dtype);
                        session('pdtype', $pdtype);
                    } else {
                        zft_log('wx/login', $memberModel->getLastSql());
                        $code = 400;
                        $content = '绑定失败，请联系相关人员';
                    }
                }
            }

            $this->apiReturn($code, [], $content);
        }
    }
}